package net.qiqb.usr.points.domain;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.qiqb.usr.points.application.CalculatePointLevelCmd;
import net.qiqbframework.commandhandling.CommandHandler;
import net.qiqbframework.config.Configuration;
import net.qiqbframework.eventhandling.EventMessage;
import net.qiqbframework.eventhandling.GenericEventMessage;
import net.qiqbframework.eventhandling.gateway.EventGateway;
import net.qiqbframework.eventsourcing.EventStorageEngine;
import net.qiqbframework.messaging.task.CurrentMessageTask;
import net.qiqbframework.modelling.command.CommandAggregateContext;
import net.qiqbframework.modelling.domain.AggregateContext;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class CalculatePointDomainService {

    private final Configuration configuration;
    /**
     * 计算积分
     *
     * @return
     */

    @CommandHandler
    public Integer calculateVipLevel(CalculatePointLevelCmd cmd) {
        configuration.getComponent(EventStorageEngine.class).appendEvents(GenericEventMessage.asEventMessage("calculateVipLevel"));
        CurrentMessageTask.get().onRollback(e ->{
            log.error("回滚");
        });
        if (cmd.getPoint() == null || cmd.getPoint().compareTo(0) < 0) {
            throw new IllegalArgumentException("积分必须大于0");
        }

        if (cmd.getPoint().compareTo(50) <= 0) {
            return 0;
        }
        if (cmd.getPoint().compareTo(80) <= 0) {
            return 1;
        }
        return 2;
    }




    @CommandHandler(commandName = "test")
    public void calculateVipLevel() {
       log.info("信号，没有任何命令载体");
    }

}
